package com.baidu.map
{
	import baidu.map.basetype.LngLat;
	import baidu.map.basetype.Size;
	import baidu.map.control.base.Navigator;
	import baidu.map.control.base.Overview;
	import baidu.map.control.base.Ruler;
	import baidu.map.control.base.Scaler;
	import baidu.map.core.Map;
	import baidu.map.layer.Layer;
	import baidu.map.layer.RasterLayer;
	import baidu.map.overlay.Label;
	
	import flash.display.Sprite;
	import flash.text.TextFormat;
	
	import mx.core.UIComponent;
	
	public class BaiduMap extends UIComponent
	{
		private var _map:Map;
		private var nav:Navigator;
		private var overview:Overview;
		private var scaler:Scaler;
		private var ruler:Ruler;
		public function BaiduMap(w:uint,h:uint)
		{
			init(w,h);
		}
		
		public function init(w:uint,h:uint):void{
			map=new Map(new Size(w,h));
			this.addChild(map);
			map.centerAndZoom(new LngLat(116.404,39.915),12);
			var layer:Layer=new RasterLayer("BaiduMap",map);
			map.addLayer(layer);
		}
		public function addOverLayLable(lat:LngLat,text:String="测试文字"):void{
			
			var bg:Sprite = new Sprite();
			bg.graphics.beginFill(0x0000ff, 0.7);
			bg.graphics.drawRect(0, 0, 100, 100);
			bg.graphics.endFill(); 
			var label:Label = new Label(text);
			if(lat==null){
				lat=new LngLat(116.404, 39.85)
			}
			label.position = lat;
			label.contentStyle = new TextFormat("宋体", 14, 0xff0000, true);
			label.background = bg;
			map.addOverlay(label);
		}
		public function updatesize(w:uint,h:uint):void{
			if(map!=null)
			map.size=new Size(w,h);
		}
		
		public function center(center:LngLat):void{
			map.centerAndZoom(center,map.zoom);
		}
		public function zoom(zoom:uint):void{
			map.centerAndZoom(map.center,zoom);
		}
		public function centerAndZoom(center:LngLat,level:uint):void{
			map.centerAndZoom(center,level);
		}
		
		public function addNavigator():void{
			if(nav==null){
				nav=new Navigator(map);
				nav.name="nav";
			}
			
			map.addControl(nav);
		}
		
		public function removeNavigator():void{
			if(map.getChildByName("nav")!=null){
				map.removeControl(nav);
			}
		}

		public function addOverview():void{
			if(overview==null){
				overview=new Overview(map);
				overview.name="overview";
			}
			
			map.addControl(overview);
		}
		
		public function removeOverview():void{
			if(map.getChildByName("overview")!=null){
				map.removeControl(overview);
			}
		}
		
		public function addScaler():void{
			if(scaler==null){
				scaler=new Scaler(map);
				scaler.name="scaler";
			}
			
			map.addControl(scaler);
		}
		
		public function removeScaler():void{
			if(map.getChildByName("scaler")!=null){
				map.removeControl(scaler);
			}
		}
		
		public function addRuler():void{
			if(ruler==null){
				ruler=new Ruler(map);
				ruler.name="ruler";
			}
			
			map.addControl(ruler);
		}
		
		public function removeRuler():void{
			if(map.getChildByName("ruler")!=null){
				map.removeControl(ruler);
			}
		}
		public function get map():Map
		{
			return _map;
		}

		public function set map(value:Map):void
		{
			_map = value;
		}

	}
}